/*
 * 加载完后执行
 */
$(function () {
    const Base_Url = "http://127.0.0.1:8080/";//配置API接口地址
    const VCodeButton = $("#VCodeButton");
    const RegistButton = $("#RegistButton");
    const mobile = $("#mobile");
    const vCode = $("#vCode");
    const password = $("#password");
    const comfirmPassword = $("#comfirmPassword");
    const alreadyRead = $("#alreadyRead");

    //发送验证码按钮是否可用
    var vCodeButtonStatus = true;
    //注册按钮是否可用
    var registButtonStatus = true;

    // 验证手机号
    var isPhoneNo = (phone) => {
        var pattern = /^1[34578]\d{9}$/;
        return pattern.test(phone);
    }

    //验证码倒计时
    var countdownVCode = () => {
        let time = 60;
        let interval = window.setInterval(() => {
            time--;
            if (time <= 0) {
                vCodeButtonStatus = true;
                VCodeButton.text("获取短信验证码");
                window.clearInterval(interval);
            } else {
                VCodeButton.text(time + "s 后重新获取");
            }
        }, 1000);
    }

    /**
     * 绑定发送验证码事件
     **/
    VCodeButton.bind("click", () => {
        //按钮可用
        if (vCodeButtonStatus) {
            //手机号码
            let mobileNum = $.trim(mobile.val());
            //简单校验一下
            if (mobileNum.length == 0) {
                alert("请输入手机号");
            } else if (!isPhoneNo(mobileNum)) {
                alert("手机号码不正确");
            } else {
                vCodeButtonStatus = false;
                VCodeButton.text("验证码发送中...");
                $.post(Base_Url + "regist/getSmsCode", {mobile: mobileNum}, (data) => {
                    //成功
                    if (data.status == "S") {
                        countdownVCode();
                    }
                    //失败
                    else {
                        alert(data.sysMsg);
                        vCodeButtonStatus = true;
                        VCodeButton.text("获取短信验证码");
                    }
                });
            }
        }
    });

    /**
     *绑定注册事件
     **/
    RegistButton.bind("click", () => {
        if(!registButtonStatus){return;}

        let mobileVal = $.trim(mobile.val());
        let vCodeVal = $.trim(vCode.val());
        let passwordVal = $.trim(password.val());
        let comfirmPasswordVal = $.trim(comfirmPassword.val());
        let alreadyReadVal = alreadyRead[0].checked;

        if (!alreadyReadVal) {
            alert("请阅读并同意《IUV用户协议》");
        } else if (mobileVal.length == 0) {
            alert("请输入手机号");
        } else if (vCodeVal.length == 0) {
            alert("请输入验证码");
        } else if (passwordVal.length == 0) {
            alert("请输入密码");
        } else if (comfirmPasswordVal.length == 0) {
            alert("请再次输入密码");
        } else if (!isPhoneNo(mobileVal)) {
            alert("手机号码不正确");
        } else if (passwordVal != comfirmPasswordVal) {
            alert("两次密码输入不一致");
        } else if (passwordVal.length < 6) {
            alert("密码长度不能少于6位");
        } else {
            registButtonStatus = false;
            RegistButton.text("注册中...");
            $.post(Base_Url + "regist/regist", {mobile: mobileVal,code:vCodeVal,password:passwordVal,comfirmPassword:comfirmPasswordVal}, (data) => {
                //成功
                if (data.status == "S") {
                    alert(mobileVal + "注册成功");
                }
                //失败
                else {
                    alert(data.sysMsg);
                }
                registButtonStatus = true;
                RegistButton.text("注册");
            });
        }
    });
});
